package com.jz.netthings.engine.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jz.netthings.appengine.baseData.Material;
import com.jz.netthings.appengine.baseData.Materialbatchno;
import com.jz.netthings.appengine.service.MaterialbatchnoService;
import com.jz.netthings.application.upload.service.UploadService;
import com.jz.netthings.bean.system.JyUser;
import com.jz.netthings.engine.bean.NurseUser;
import com.jz.netthings.exception.AdscmSystemException;
import com.jz.netthings.system.service.JyUserService;
import com.jz.netthings.system.utils.AdscmSystemUtils;
import com.jz.netthings.utils.AdscmBaseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

@RestController
@RequestMapping("/api")
public class JzMachineReqController {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    JyUserService jyUserService;
    @Autowired
    MaterialbatchnoService materialbatchnoService;
    @Value("${ly.upload.baseUrl}")
    String  photoUrl;
    @Autowired
    UploadService uploadService;

    @RequestMapping("/users")
    public List<NurseUser> machineNurse() throws AdscmSystemException {
        List<NurseUser> nurseUsers = new ArrayList<NurseUser>();

        Wrapper<JyUser> wrapper = new EntityWrapper<>();
        wrapper.eq("unit_id",2);
        List<JyUser> jyUsers = jyUserService.selectList(wrapper);

        if (jyUsers != null && jyUsers.size()>0){
            for (JyUser user :
                    jyUsers) {
                NurseUser nurseUser = new NurseUser();
                nurseUser.setId(user.getId()+"");
                nurseUser.setName(user.getName());
                String gender = user.getGender();
                if (AdscmBaseUtils.getInstance().isNotEmpty(gender,null)){
                    if ("1".equals(gender)){
                        gender = "男";
                    }else {
                        gender = "女";
                    }
                    nurseUser.setSex(gender);
                }
                nurseUser.setPhone(user.getPhone());
                String userPhoto = user.getPhoto();
                byte[] photo = null;
                if (AdscmBaseUtils.getInstance().isNotEmpty(userPhoto,null)){
                    photo = uploadService.downloadImageReturnBytes(userPhoto);
                    if (photo == null){
                        photo = AdscmBaseUtils.getInstance().getNullNursePhoto();
                    }
//                    photo = AdscmSystemUtils.getInstance().getFileBytes(userPhoto);
                }else {
                    photo = AdscmBaseUtils.getInstance().getNullNursePhoto();
                }
                photo = AdscmBaseUtils.getInstance().buildIcon(photo, 100, 100);
                nurseUser.setPhoto(photo);

                nurseUsers.add(nurseUser);
            }
        }




        return nurseUsers;
    }

    /**
     * 耗材、抗凝剂、盐水厂家及批号信息
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/materials")
    public List<Material> machineMaterials() throws AdscmSystemException {
        List<Materialbatchno> list = materialbatchnoService.selectList(null);
        logger.info("单采机查询耗材、抗凝剂、盐水厂家及批号信息...");
        List<Material> materials = new ArrayList<Material>();
        if (list != null && list.size()>0){
            for (Materialbatchno batchNo:list) {
                Material material = new Material();
                material.setDonorId(batchNo.getCode());
                Integer mcid = batchNo.getMcid();
                if (mcid != null && mcid > -1){
                    String mcName = materialbatchnoService.getMcName(mcid);
                    material.setMachineUserId(mcName);
                }
                material.setMachineFactoryId("");
                materials.add(material);
            }
        }
        return materials;
    }
}
